
import {getUser} from '../../api/wxh/getData.js'
import {getStore, setStore, removeStore} from '../../api/wxh/session.js'



const RECORD_SESSIONID = 'RECORD_SESSIONID'
const ClEAR_SESSIONID = 'ClEAR_SESSIONID'
const USER_INFO	= 'USER_INFO'
const LOGO_IN = 'LOGO_IN'
const LOGO_OUT = 'LOGO_OUT'
const CLEAR_INFO = 'CLEAR_INFO'
const RANDOM = 'RANDOM'

const xhModules = {
	namespaced: true,
	state: {
		sessionId: null,
		login: false,
		userInfo: {},
		random: null,
	},
	getters: {
		sessionId: state => {
			return state.sessionId;
		},
		userInfo: state => {
			return state.userInfo;
		}
		,
		login: state => {
			return state.login;
		}
	},
	actions: {
		async getUserInfo({commit, state}) {
			let res = await getStore('sessionId');

			if(res === 'null' || res === null) {
				return
			}
			commit(RECORD_SESSIONID, res);
			commit(LOGO_IN, true);
		}
	},
	mutations: {
		[RECORD_SESSIONID](state, info) {
				// if(!info){
				// 	removeStore('sessionId');
				// 	return
				// }
                state.sessionId = info;
                // console.log(state.sessionId,'state.sessionId')
				// setStore('sessionId', info);
		},
		[ClEAR_SESSIONID](state, info) {
			state.sessionId = null;
			removeStore('sessionId');
		},
		[USER_INFO](state, info) {
			// state.userInfo = info;
			// let _info = state.userInfo || {};
			console.log("mutation--->", info);
			state.userInfo = Object.assign({}, state.userInfo, info);
            // console.log(state.userInfo,'state.userInfo')
		},
		[CLEAR_INFO](state, info){
			state.userInfo = info;
		}
		,
		[LOGO_IN](state, info) {
			state.login = info;
		},
		[LOGO_OUT](state, info) {
			state.login = info;
		},
		[RANDOM](state, info) {
			state.random = info;
		}
	}
}

export default xhModules;
